--[[ A million thanks to Antiarc (cheald@gmail dot com) for the Omen addon from which I pulled nearly all of this code ]]--
local MINOR_VERSION = tonumber(("$Revision: 1$"):match("%d+"))
-- geh local PlayerName = UnitName("player")
-- geh local Threat = LibStub("Threat-2.0")
-- geh local math_floor = math.floor
local timers = {}
local AceConfig = LibStub("AceConfigDialog-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("EQdkpRT")

EQdkpRT = LibStub("AceAddon-3.0"):NewAddon("EQdkpRT", "AceEvent-3.0", "AceTimer-3.0", "AceBucket-3.0", "AceConsole-3.0", "LibSink-2.0")
EQdkpRT.MINOR_VERSION = MINOR_VERSION
EQdkpRT.title = "EQdkpRT"
EQdkpRT.version = GetAddOnMetadata(EQdkpRT.title, "Version")

-- Silently fail embedding if it doesn't exist
LibStub("AceAddon-3.0"):EmbedLibrary(EQdkpRT, "LibFuBarPlugin-Mod-3.0", true)

EQdkpRT:SetDefaultModuleState(false)
EQdkpRT.SkinList = {}

local optFrame
function EQdkpRT:OnInitialize()
  self.db = LibStub("AceDB-3.0"):New("EQdkpRTDB")
  self.db:RegisterDefaults({
		global = {
			skins = {}
		},
		profile = {
			activeSkin = {},
			options = {},
			sinkOptions = {}
		}
	})
	self:SetSinkStorage(self.db.profile.sinkOptions)
	self.configOptions.args.output = self:GetSinkAce3OptionsDataTable()
	self:CreateFrame()
	if self.Anchor:GetHeight() < 120 then
		self.Anchor:SetHeight(120)
	end
	EQdkpRT:PopulateSkinList()

	if LibStub:GetLibrary("LibFuBarPlugin-Mod-3.0", true) then
		-- Create the FuBarPlugin bits.
		self:SetFuBarOption("GameTooltip", true)
		self:SetFuBarOption("hasNoColor", true)
		self:SetFuBarOption("cannotDetachTooltip", true)
		self:SetFuBarOption("hideWithoutStandby", true)
		self:SetFuBarOption("cannotDetachTooltip", true)
		self:SetFuBarOption("iconPath", [[Interface\AddOns\EQdkpRT\icon]])
	end

	optFrame = AceConfig:AddToBlizOptions("EQdkpRT", "EQdkpRT")

	self.configOptions.args.profile = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
	self:UpdateVisible()
	self:SetAnchors(true)

	if self:GetOption("Standby") then
		self:SetEnabledState(false)
		self:Disable()
	end
end

function EQdkpRT:OnUpdateFuBarTooltip()
	GameTooltip:AddLine("|cffffff00" .. L["Click|r to toggle the Omen window"])
	GameTooltip:AddLine("|cffffff00" .. L["Right-click|r to open the options menu"])
end

function EQdkpRT:OnFuBarClick(button)
	self:Toggle()
	EQdkpRT:SetOption("HardOff", not EQdkpRTAnchor:IsVisible())
end

local modIdx = {}
function EQdkpRT:CycleModule(dir)
	local ct, activeIdx = 0, 0
	for k, v in self:IterateModules() do
		ct = ct + 1
		modIdx[ct] = v
		if v:IsEnabled() then
			activeIdx = ct
		end
	end

	local newIdx
	if dir == -1 then
		newIdx = activeIdx - 1
		if newIdx == 0 then
			newIdx = #modIdx
		end
		modIdx[newIdx]:Enable()
	else
		newIdx = activeIdx + 1
		if newIdx == #modIdx + 1 then
			newIdx = 1
		end
		modIdx[newIdx]:Enable()
	end
end

function EQdkpRT:EnableLastModule()
	local module = self:GetOption("Modules.LastActive")
	if module then
		-- self:DisableModule(module)
		self:EnableModule(module)
	else
		for k, v in self:IterateModules() do
			-- v:Disable()
			v:Enable()
			break
		end
	end
end

function EQdkpRT:DisableModules()
	for k, v in self:IterateModules() do
		v:Disable()
	end
end

function EQdkpRT:OnEnable()
	self:LayoutModuleIcons()
	self:EnableLastModule()

	self:Toggle(self:GetOption("Show"))

-- geh	Threat.RegisterCallback(self, "Activate", "UpdateVisible")
-- geh	Threat.RegisterCallback(self, "Deactivate", "UpdateVisible")

	if LibStub:GetLibrary("LibFuBarPlugin-Mod-3.0", true) and self:GetOption("FuBar.HideMinimapButton", false) then
		self:Hide()
	end
end

function EQdkpRT:OnDisable()
-- geh	Threat.UnregisterCallback(self, "Activate")
-- geh	Threat.UnregisterCallback(self, "Deactivate")
	self.Anchor:Hide()
end

function EQdkpRT:Test()
	self = EQdkpRT
	if self.activeModule then
		self.activeModule:Test()
	end
end

function EQdkpRT:Toggle(setting)
	if not EQdkpRT.Anchor then return end
	if setting ~= nil then
		self:SetOption("Show", setting)
	else
		self:SetOption("Show", not EQdkpRT.Anchor:IsVisible())
	end
	if self:GetOption("Show") then
		self.Anchor:Show()
-- geh		self:EnableLastModule()
	else
		self.Anchor:Hide()
		self:DisableModules()
	end
end

local setupUIFrame = false
function EQdkpRT:ShowConfig()
--[[ geh no idea why I would want to open default Ace config panel
	if IsModifierKeyDown() then
		InterfaceOptionsFrame:SetWidth(700)
		if not setupUIFrame then
			InterfaceOptionsFrame:SetMovable(true)
			InterfaceOptionsFrame:SetScript("OnMouseDown", function() this:StartMoving() end)
			InterfaceOptionsFrame:SetScript("OnMouseUp", function() this:StopMovingOrSizing() end)
			setupUIFrame = true
		end
		InterfaceOptionsFrame_OpenToFrame(optFrame)
	else
--		AceConfig:SetDefaultSize("EQdkpRT", 500, 550)
--    AceConfig:Open("EQdkpRT", configFrame)
	end
]]
  InterfaceOptionsFrame_OpenToFrame("EQdkpRT")
end
EQdkpRT.OpenMenu = EQdkpRT.ShowConfig		-- for fubar

function EQdkpRT:ShowDropdown()
	local dd = LibStub("LibDropdown-1.0")
	if dd then
		dd:OpenAce3Menu(self.configOptions)
	end
end
